package com.itheima.sh.web;

import com.itheima.sh.pojo.Role;
import com.itheima.sh.pojo.User;
import com.itheima.sh.service.RoleMapperImpl;
import com.itheima.sh.service.UserServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class WebApp {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true) {
            try {
                // 需求：分页查询用户和对应的角色信息
                // queryAllUsersByPage(sc);

                // 分页查询角色以及对应的用户和权限信息
                // queryAllUsersAndRoleAndPermissionByPage(sc);

                // # 分页查询角色和权限信息
                // queryAllRoleAndPermissionByPage(sc);

                // 需求：修改id是1的用户的密码为456，用户名为刘德华，扮演角色变为游客(提示：游客角色id是3)和会员(提示：会员角色id是2)
                 updateUserAndRoles(sc);
            } catch (Exception e) {
                System.out.println("数据异常,请根据要求填写数据");
                break;
            }
        }

    }

    private static void updateUserAndRoles(Scanner sc) {
        User user = new User();
        System.out.println("请输入要修改的id");
        int id = sc.nextInt();
        System.out.println("请输入修改后的用户名");
        String userName = sc.next();
        System.out.println("请输入要修改的密码");
        String passWord = sc.next();
        System.out.println("请输入修改后的邮箱");
        String email = sc.next();

        ArrayList<Integer> roleIndexs = new ArrayList<>();

        System.out.println("是否继续清空之前角色信息并重新指定角色信息y/n");
        int i = 0;
        while (i < 3 ) {
            System.out.println("是否继续添加(最多添加三次)y/n");
            String line = sc.next();
            if (line.equalsIgnoreCase("y")) {
                System.out.println("请输入要指定的角色信息(可多选)(1 管理员 2 会员 3 游客)");
                int roleIndex = sc.nextInt();
                roleIndexs.add(roleIndex);
                i++;
            }else {
                break;
            }
        }
        user.setId(id);
        user.setUsername(userName);
        user.setPassword(passWord);
        user.setEmail(email);

        UserServiceImpl userService = new UserServiceImpl();
        // 1.根据id修改用户信息update
        userService.updateUser(user);
        // 2.根据id删除用户角色中间表的信息
        userService.deleteRole(id);
        // 3.根据id增加用户角色中间表
        for (Integer roleIndex : roleIndexs) {
            userService.addRoles(id, roleIndex);
        }
    }

    private static void queryAllRoleAndPermissionByPage(Scanner sc) {
        System.out.println("请输入起始索引");
        int startIndex = sc.nextInt();
        System.out.println("请输入每页条数");
        int pageSize = sc.nextInt();

        RoleMapperImpl roleMapper = new RoleMapperImpl();

        List<Role> list = roleMapper.queryAllRoleAndPermissionByPage(startIndex, pageSize);

        for (Role role : list) {
            System.out.println(role);
        }
    }

    private static void queryAllUsersAndRoleAndPermissionByPage(Scanner sc) {
        System.out.println("请输入起始索引");
        int startIndex = sc.nextInt();
        System.out.println("请输入每页条数");
        int pageSize = sc.nextInt();

        RoleMapperImpl roleMapper = new RoleMapperImpl();

        List<Role> list = roleMapper.queryAllUsersAndRoleAndPermissionByPage(startIndex, pageSize);

        for (Role role : list) {
            System.out.println(role);
        }
    }

    private static void queryAllUsersByPage(Scanner sc) {
        System.out.println("请输入起始索引");
        int startIndex = sc.nextInt();
        System.out.println("请输入每页条数");
        int pageSize = sc.nextInt();

        UserServiceImpl userService = new UserServiceImpl();
        List<User> list = userService.queryAllUsersByPage(startIndex, pageSize);

        for (User user : list) {
            System.out.println(user);
        }
    }

}
